package cn.service;

import cn.mapper.StudentMapper;
import cn.pojo.Page;
import cn.pojo.Student;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentImpl implements StudentService{
    @Autowired
    private StudentMapper mapper;

    @Override
    public Page findByPage(Student student, String url, Integer pageNum) {
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        if (student.getName()!=null&&!("").equals(student.getName())) {
            criteria.andLike("name","%"+student.getName()+"%");
        }

        int totalNum = mapper.selectCountByExample(example);
        Page page = new Page(pageNum,totalNum,url);
        List<Student> students = mapper.selectByExampleAndRowBounds(example,new RowBounds(page.getStartIndex(),page.getPageSize()));
        page.setList(students);
        return page;
    }
}
